package net.starwifi.duobao.web.db;

import net.starwifi.duobao.entity.DBOrder;
import net.starwifi.duobao.entity.Member;
import net.starwifi.duobao.entity.Receiver;
import net.starwifi.duobao.mvc.*;
import net.starwifi.duobao.service.DBOrderService;
import net.starwifi.duobao.service.MemberService;
import net.starwifi.duobao.service.ReceiverService;
import net.starwifi.duobao.utils.kd.KDHelper;
import net.starwifi.duobao.utils.kd.KuaiDiDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户夺宝记录
 * Created by Administrator on 2015/11/21.
 */
@Controller("apiDBMemberRecordController")
@RequestMapping("/member/dbRecord")
public class UserRecordController extends BaseController {

    @Autowired
    MemberService memberService;

    @Autowired
    DBOrderService dbOrderService;

    @Autowired
    ReceiverService receiverService;

    /**
     * 我的中奖详情信息
     */
    @RequestMapping(value = "/winDetail-{id}", method = RequestMethod.GET)
    public String productDetail(Model model,@PathVariable Long id,Long receiverId) {
        DBOrder order=dbOrderService.find(id);
        Member member=memberService.getCurrent();
        if(order==null){
            return "common/error";
        }
        if(!order.getMemberId().equals(member.getId())){
            return "common/error";
        }
        if(receiverId!=null){
            Receiver receiver=receiverService.find(receiverId);
            if(receiver!=null&&receiver.getMember().getId().equals(member.getId())&&order.getShippingDate()==null){
                order.setConsignee(receiver.getConsignee());
                order.setAreaName(receiver.getAreaName());
                order.setAddress(receiver.getAddress());
                order.setShippingDate(new Date());
                dbOrderService.update(order);
            }
        }
        if(order.getShippingSn()!=null){
            String json= KDHelper.getJson(order.getShippingSn());
            KuaiDiDTO kuaiDiDTO=KuaiDiDTO.parse(json);
            if(kuaiDiDTO!=null&&kuaiDiDTO.getTraces()!=null){
                if(kuaiDiDTO.getTraces().size()>0){
                    model.addAttribute("traces",kuaiDiDTO.getTraces());
                }
            }
        }
        model.addAttribute("entity",order);
        return "/db/winDetail";
    }



    /**
     * 我的中奖详情信息
     */
    @RequestMapping(value = "/winDetail-{id}-sh", method = RequestMethod.GET)
    public String productDetailSH(Model model,@PathVariable Long id) {
        DBOrder order=dbOrderService.find(id);
        Member member=memberService.getCurrent();
        if(order==null){
            return "common/error";
        }
        if(!order.getMemberId().equals(member.getId())){
            return "common/error";
        }
        if(order.getShippingStartDate()==null){
            return "common/error";
        }
        order.setShippingEndDate(new Date());
        order.setShippingStatus(2);
        dbOrderService.update(order);
        return "redirect:/member/dbRecord/winDetail-"+order.getId();
    }


    /**
     * 我的中奖记录
     */
    @RequestMapping(value = "/lotteryList", method = RequestMethod.GET)
    public String list(Model model) {
        Member member=memberService.getCurrent();
        List<net.starwifi.duobao.mvc.Order> orderBy=new ArrayList<Order>();
        orderBy.add(net.starwifi.duobao.mvc.Order.desc("id"));
        List<DBOrder> orderList=dbOrderService.getListData("o.paymentStatus=?1 and o.memberId=?2 and o.isLottery=?3", new Object[]{2, member.getId(),Boolean.TRUE}, orderBy);
        model.addAttribute("orderList",orderList);
        return "/db/userRecord_lottery";
    }


    /**
     * 我的夺宝码
     */
    @ResponseBody
    @RequestMapping(value = "/getCode", method = RequestMethod.GET)
    public Object getCode(Long orderId) {
        Member member=memberService.getCurrent();
        DBOrder dbOrder=dbOrderService.find(orderId);
        if(!dbOrder.getMemberId().equals(member.getId())){
            return Message.error("请求非法");
        }
        return Message.success(dbOrder.getCodeInfo());
    }

}
